using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Microsoft.Build.Framework;
using System.Collections;

namespace Ardal.MSBuildProfiler.Test
{
  [TestFixture]
  public class MSBuildProfilerTest
  {
    private bool EventCalled;
    public void TestMSBuildProfiler()
    {
      TestEventSource eventSource = new TestEventSource();
      MSBuildProfiler profiler = new MSBuildProfiler(eventSource);
      profiler.ProfilerFinished += new ProfilerFinishedEventHandler(TestProfilerFinished);
      eventSource.Play();
      Assert.IsTrue(EventCalled);
    }

    private void TestProfilerFinished(Build build)
    {
      Assert.IsNotNull(build);
      AssertItem(build, 1, "helpKeyword", "MSBuild", "Test build started", "MSBuild");
      Assert.IsTrue(build.Children[0] is Project);
      Project project = build.Children[0] as Project;
      Assert.IsNotNull(project);
      AssertItem(project, 1, "helpKeyword", "projectFile", "Test project started", "MSBuild");
      Assert.IsTrue(project.Children[0] is Target);
      Target target = project.Children[0] as Target;
      Assert.IsNotNull(target);
      AssertItem(target, 1, "helpKeyword", "targetName:Target", "Test target started", "MSBuild");
      Assert.IsTrue(target.Children[0] is Task);
      Task task = target.Children[0] as Task;
      Assert.IsNotNull(task);
      AssertItem(task, 0, "helpKeyword", "taskName:Task", "Test task started", "MSBuild");
      EventCalled = true;
    }

    private static void AssertItem(Item item, int childrenCount, string helpKeyword, string itemName, string message, string senderName)
    {
      Assert.AreEqual(childrenCount, item.Children.Count);
      Assert.AreEqual(helpKeyword, item.HelpKeyword);
      Assert.AreEqual(itemName, item.ItemName);
      Assert.AreEqual(message, item.Message);
      Assert.AreEqual(senderName, item.SenderName);
    }
  }

  public class TestEventSource : IEventSource
  {
    #region IEventSource Members

    public event AnyEventHandler AnyEventRaised;

    public event BuildFinishedEventHandler BuildFinished;

    public event BuildStartedEventHandler BuildStarted;

    public event CustomBuildEventHandler CustomEventRaised;

    public event BuildErrorEventHandler ErrorRaised;

    public event BuildMessageEventHandler MessageRaised;

    public event ProjectFinishedEventHandler ProjectFinished;

    public event ProjectStartedEventHandler ProjectStarted;

    public event BuildStatusEventHandler StatusEventRaised;

    public event TargetFinishedEventHandler TargetFinished;

    public event TargetStartedEventHandler TargetStarted;

    public event TaskFinishedEventHandler TaskFinished;

    public event TaskStartedEventHandler TaskStarted;

    public event BuildWarningEventHandler WarningRaised;

    #endregion

    public void Play()
    {
      BuildStarted("Test", new BuildStartedEventArgs("Test build started", "helpKeyword"));
      ProjectStarted("Test", new ProjectStartedEventArgs("Test project started", "helpKeyword", "projectFile", "targetNames", new ArrayList(), new ArrayList()));
      TargetStarted("Test", new TargetStartedEventArgs("Test target started", "helpKeyword", "targetName", "projectFile", "targetFile"));
      TaskStarted("Test", new TaskStartedEventArgs("Test task started", "helpKeyword", "projectFile", "taskFile", "taskName"));
      TaskFinished("Test", new TaskFinishedEventArgs("Test task finished", "helpKeyword", "projectFile", "taskFile", "taskName", true));
      TargetFinished("Test", new TargetFinishedEventArgs("Test target finished", "helpKeyword", "targetName", "projectFile", "targetFile", true));
      ProjectFinished("Test", new ProjectFinishedEventArgs("Test project finished", "helpKeyword", "projectFile", true));
      BuildFinished("Test", new BuildFinishedEventArgs("Test build finished", "helpKeyword", true));
    }
  }
}
